;Jobe's Launcher Plus Installer NSIS Script
;derived from:
;NSIS Modern User Interface
;Basic Example Script
;Written by Joost Verburg
;
; Microsoft .NET Framework Version 2.0 Redistributable Package (x86):
; http://download.microsoft.com/download/5/6/7/567758a3-759e-473e-bf8f-52154438565a/dotnetfx.exe
;
;--------------------------------
;Includes

  !include "MUI.nsh"
  !include "CheckDotNetFramework.nsh"

;--------------------------------
;Functions

!define JLP_VERSION "0_1_36"
!define JLP_DEFAULT_INSTALL "LauncherPlus"
!define JLP_DEFAULT_STARTMENU "Launcher Plus (BETA)"

!define LVM_GETITEMCOUNT 0x1004
!define LVM_GETITEMTEXT 0x102D

Function DumpLog
  Exch $5
  Push $0
  Push $1
  Push $2
  Push $3
  Push $4
  Push $6

  FindWindow $0 "#32770" "" $HWNDPARENT
  GetDlgItem $0 $0 1016
  StrCmp $0 0 error
  FileOpen $5 $5 "w"
  StrCmp $5 0 error
    SendMessage $0 ${LVM_GETITEMCOUNT} 0 0 $6
    System::Alloc ${NSIS_MAX_STRLEN}
    Pop $3
    StrCpy $2 0
    System::Call "*(i, i, i, i, i, i, i, i, i) i \
      (0, 0, 0, 0, 0, r3, ${NSIS_MAX_STRLEN}) .r1"
    loop: StrCmp $2 $6 done
      System::Call "User32::SendMessageA(i, i, i, i) i \
        ($0, ${LVM_GETITEMTEXT}, $2, r1)"
      System::Call "*$3(&t${NSIS_MAX_STRLEN} .r4)"
      FileWrite $5 "$4$\r$\n"
      IntOp $2 $2 + 1
      Goto loop
    done:
      FileClose $5
      System::Free $1
      System::Free $3
      Goto exit
  error:
    MessageBox MB_OK error
  exit:
    Pop $6
    Pop $4
    Pop $3
    Pop $2
    Pop $1
    Pop $0
    Exch $5
FunctionEnd

Function InstallDotNetFramework

  ; download and run framework installer
  ; alternative URL: http://www.microsoft.com/downloads/details.aspx?familyid=0856EACB-4362-4B0D-8EDD-AAB15C5E04F5&displaylang=en
  NSISdl::download "http://download.microsoft.com/download/5/6/7/567758a3-759e-473e-bf8f-52154438565a/dotnetfx.exe" "$TEMP\Microsoft .NET Framework 2.0 Redistributable (x86).exe"
  ExecWait '"$TEMP\Microsoft .NET Framework 2.0 Redistributable (x86).exe"'

FunctionEnd

Function RunLauncherPlus
  Exec '"$INSTDIR\LauncherPlus.exe"'
FunctionEnd

;--------------------------------
;General

  ;Name and file
  Name "Jobe's Launcher Plus"
  OutFile "..\LauncherPlusSetup${JLP_VERSION}.exe"
  
  ;Default installation folder
  InstallDir "$PROGRAMFILES\${JLP_DEFAULT_INSTALL}"

  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "Software\LauncherPlus" ""

;--------------------------------
;Variables

  Var MUI_TEMP
  Var STARTMENU_FOLDER

;--------------------------------
;Interface Settings

  !define MUI_ABORTWARNING
  !define MUI_ICON "LauncherPlus.ico"
  !define MUI_UNICON "LauncherPlus.ico"

;--------------------------------
;Pages

  !define MUI_WELCOMEFINISHPAGE_BITMAP "Branding.bmp"
  !define MUI_UNWELCOMEFINISHPAGE_BITMAP "Branding.bmp"
  !insertmacro MUI_PAGE_WELCOME
  !insertmacro MUI_PAGE_LICENSE "LauncherPlusLicense.txt"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  ;Start Menu Folder Page Configuration
    !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKCU"
    !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\LauncherPlus"
    !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
    !define MUI_STARTMENUPAGE_DEFAULTFOLDER "${JLP_DEFAULT_STARTMENU}"
  !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  !insertmacro MUI_PAGE_INSTFILES
    !define MUI_FINISHPAGE_RUN
    !define MUI_FINISHPAGE_RUN_CHECKED
    !define MUI_FINISHPAGE_RUN_TEXT "Start LauncherPlus"
    !define MUI_FINISHPAGE_RUN_FUNCTION "RunLauncherPlus"
  !insertmacro MUI_PAGE_FINISH

  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"
;  !insertmacro MUI_LANGUAGE "German"

;--------------------------------
;Installer Sections

Section "!LauncherPlus" SecMain

  SectionIn RO
 
  ; check .NET Framework
  Call CheckDotNetFramework
  
  ; set path to install files to
  SetOutPath "$INSTDIR"
  
  ;Main Application
  File "..\bin\LauncherPlus.exe"
  
  ;librarys
  File "..\bin\devil.dll"
  File "..\bin\ICSharpCode.SharpZipLib.dll"
  File "..\bin\ilu.dll"
  File "..\bin\ilut.dll"
  File "..\bin\MessageBoxHook.dll"
  File "..\bin\PlugIn.dll"

  ;language
  File "..\bin\lang[EN].resources"

  ;help
  File "..\help\LauncherPlus.chm"

  ; license
  File "LauncherPlusLicense.txt"

  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application

  CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"

  ; startmenu shortcut
  CreateShortCut \
    "$SMPROGRAMS\$STARTMENU_FOLDER\LauncherPlus.lnk" \
    "$INSTDIR\LauncherPlus.exe" \
    "" \
    "" "" \
    SW_SHOWNORMAL \
    "" \
    "An improved launcher for Blizzards Word of Warcraft"

  ; help shortcut
  CreateShortCut \
    "$SMPROGRAMS\$STARTMENU_FOLDER\LauncherPlus - Help.lnk" \
    "$INSTDIR\LauncherPlus.chm" \
    "" \
    "$INSTDIR\LauncherPlus.exe" 3 \
    SW_SHOWNORMAL \
    "" \
    "Help for Launcher Plus"

  ; startmenu shortcut
  CreateShortCut \
    "$SMPROGRAMS\$STARTMENU_FOLDER\LauncherPlus - Uninstall.lnk" \
    "$INSTDIR\LauncherPlusUninstall.exe" \
    "" \
    "" "" \
    SW_SHOWNORMAL \
    "" \
    "Uninstall Launcher Plus"

  ; desktop shortcut
  CreateShortCut \
    "$DESKTOP\LauncherPlus.lnk" \
    "$INSTDIR\LauncherPlus.exe" \
    "" \
    "" "" \
    SW_SHOWNORMAL \
    "" \
    "An improved launcher for Blizzards Word of Warcraft"
  !insertmacro MUI_STARTMENU_WRITE_END

  ; register .bwa files
  ; back up old value of .bwa
  !define Index "Line${__LINE__}"
  ReadRegStr $1 HKCR ".bwa" ""
  StrCmp $1 "" "${Index}-NoBackup"
  StrCmp $1 "LauncherPlus" "${Index}-NoBackup"
  WriteRegStr HKCR ".bwa" "backup_val" $1
  "${Index}-NoBackup:"
  WriteRegStr HKCR ".bwa" "" "LauncherPlus"
  ReadRegStr $0 HKCR "LauncherPlus" ""
  StrCmp $0 "" 0 "${Index}-Skip"
  WriteRegStr HKCR "LauncherPlus" "" "Blizzard WOW Addon"
  WriteRegStr HKCR "LauncherPlus\shell" "" "open"
  WriteRegStr HKCR "LauncherPlus\DefaultIcon" "" "$INSTDIR\LauncherPlus.exe,1"
  "${Index}-Skip:"
  WriteRegStr HKCR "LauncherPlus\shell\open\command" "" '$INSTDIR\LauncherPlus.exe "%1"'
;  WriteRegStr HKCR "LauncherPlus\shell\view" "" "View Addon with standard Zip"
;  WriteRegStr HKCR "LauncherPlus\shell\view\command" "" '$INSTDIR\execute.exe "%1"'
  System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
  !undef Index

    ;Store installation folder
  WriteRegStr HKCU "Software\LauncherPlus" "" $INSTDIR
  
  ;Create uninstaller
  WriteUninstaller "$INSTDIR\LauncherPlusUninstall.exe"

  Push "$DESKTOP\LauncherPlus-Install.log"
  Call DumpLog
  
SectionEnd

SubSection /e "Plugins" SecPlugin
  Section "Screenshots" SecScreenshots
    ; set path to install plugins to
    SetOutPath "$INSTDIR\plugins"
    File "..\bin\plugins\Screenshots.dll"
  SectionEnd
SubSectionEnd




;--------------------------------
;Descriptions

  ;Language strings
  LangString DESC_SecMain ${LANG_ENGLISH} "Launcher Plus core components. Requires 5,7 MB."
  LangString DESC_SecPlugin ${LANG_ENGLISH} "Plugins for Launcher Plus."
  LangString DESC_SecScreenshots ${LANG_ENGLISH} "Plugin for handling your screenshots. Requires 64 KB."

  ;Assign language strings to sections
  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${SecMain} $(DESC_SecMain)
    !insertmacro MUI_DESCRIPTION_TEXT ${SecPlugin} $(DESC_SecPlugin)
    !insertmacro MUI_DESCRIPTION_TEXT ${SecScreenshots} $(DESC_SecScreenshots)
  !insertmacro MUI_FUNCTION_DESCRIPTION_END

;--------------------------------
;Uninstaller Section

Section "Uninstall"

  ;Main Application
  Delete "$INSTDIR\LauncherPlus.exe"
  
  ;librarys
  Delete "$INSTDIR\devil.dll"
  Delete "$INSTDIR\lang[EN].resources"
  Delete "$INSTDIR\ICSharpCode.SharpZipLib.dll"
  Delete "$INSTDIR\ilu.dll"
  Delete "$INSTDIR\ilut.dll"
  Delete "$INSTDIR\MessageBoxHook.dll"
  Delete "$INSTDIR\PlugIn.dll"

  ; Help
  Delete "$INSTDIR\LauncherPlus.chm"

  ; license
  Delete "$INSTDIR\LauncherPlusLicense.txt"

  ; settings
  Delete "$INSTDIR\LauncherPlus.exe.config"

  ; shortcuts
  !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
  Delete "$DESKTOP\LauncherPlus.lnk"
  Delete "$SMPROGRAMS\$MUI_TEMP\LauncherPlus - Help.lnk"
  Delete "$SMPROGRAMS\$MUI_TEMP\LauncherPlus - Uninstall.lnk"
  Delete "$SMPROGRAMS\$MUI_TEMP\LauncherPlus.lnk"
  
  ;Delete empty start menu parent diretories
  StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
  startMenuDeleteLoop:
    ClearErrors
    RMDir $MUI_TEMP
    GetFullPathName $MUI_TEMP "$MUI_TEMP\.."
    IfErrors startMenuDeleteLoopDone
    StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
  startMenuDeleteLoopDone:

  ;restore old .bwa files
  !define Index "Line${__LINE__}"
  ReadRegStr $1 HKCR ".bwa" ""
  StrCmp $1 "LauncherPlus" 0 "${Index}-NoOwn" ; only do this if we own it
  ReadRegStr $1 HKCR ".bwa" "backup_val"
  StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
  DeleteRegKey HKCR ".bwa"
  Goto "${Index}-NoOwn"
  "${Index}-Restore:"
  WriteRegStr HKCR ".bwa" "" $1
  DeleteRegValue HKCR ".bwa" "backup_val"
  DeleteRegKey HKCR "LauncherPlus" ;Delete key with association settings
  System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
  "${Index}-NoOwn:"
  !undef Index

  ; uninstaller itself
  Delete "$INSTDIR\LauncherPlusUninstall.exe"

  DeleteRegKey /ifempty HKCU "Software\LauncherPlus"

SectionEnd
